/*
 * Copyright (c) 2007 Compl Yue Still, All Rights Reserved.
 */

package oex.ui
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	
	import mx.core.*;

	internal class FreePlate extends UIComponent
	{
		internal static const TERRITORY_CHANGE:String = "territoryChange";

		public function FreePlate():void
		{
			this.graphics.lineStyle(1, 0xFF0000, 0.8);
			
			this.graphics.moveTo(-20, 0);
			this.graphics.lineTo(25, 0);
			this.graphics.lineTo(20, -5);
			this.graphics.moveTo(25, 0);
			this.graphics.lineTo(20, 5);
			
			this.graphics.moveTo(0, -15);
			this.graphics.lineTo(0, 15);
		}

		public function getTerritoryBounds():EdgeMetrics
		{
			const MARGIN:int = 100;
			
			var l:int=0,t:int=0;
			var r:int=0,b:int=0;
			
			var n:int = numChildren;
			for(var i:int=0; i < n; i++)
			{
				var child:DisplayObject = getChildAt(i);

				l = Math.min(l, child.x);
				t = Math.min(t, child.y);

				var wPref:int, hPref:int;
				if(child is IUIComponent)
				{
					wPref = IUIComponent(child).getExplicitOrMeasuredWidth();
					hPref = IUIComponent(child).getExplicitOrMeasuredHeight();
				}
				else
				{
					wPref = child.width;
					hPref = child.height;
				}
				r = Math.max(r, child.x + wPref);
				b = Math.max(b, child.y + hPref);
			}
			
			return new EdgeMetrics(l-MARGIN,t-MARGIN, r+MARGIN, b+MARGIN);
		}
		
		public function invalidateTerritory():void
		{
			this.dispatchEvent(new Event(TERRITORY_CHANGE));
		}

	}
}
